/*
 *  Filters.cpp
 *  breemlib
 *
 *  Created by James on 8/4/08.
 *  Copyright 2008 __MyCompanyName__. All rights reserved.
 *
 */

#include "Filters.h"
#include "hq2x.h"

#define INTERPOLATE(p1,p2)		\
	(((p1 & 0x0000FF) + (p2 & 0x0000FF) >> 1) & 0x0000FF) |		\
	(((p1 & 0x00FF00) + (p2 & 0x00FF00) >> 1) & 0x00FF00) |		\
	(((p1 & 0xFF0000) + (p2 & 0xFF0000) >> 1) & 0xFF0000)

#define _50PERCENT(p)	(((p) >> 1) & 0x7F7F7F)

void Draw2x(unsigned long *S,unsigned long *D,int W,int H,int Pitch)
{
	int x,y;

	for(y=0;y<(H-1);y++) {
		for(x=0;x<W;x++) {
			register int x1,x2;

			x1 = x * 2 + 0;
			x2 = x * 2 + 1;
			D[x1] = S[x];
			D[x2] = S[x];
			D[x1 + Pitch] = S[x];
			D[x2 + Pitch] = S[x];
		}
		D += Pitch * 2;
		S += W;
	}
}

void Scanline2x(unsigned long *S,unsigned long *D,int W,int H,int Pitch)
{
	int x,y;

	for(y=0;y<H;y++) {
		for(x=0;x<W;x++) {
			register int x1,x2;

			x1 = x * 2 + 0;
			x2 = x * 2 + 1;
			D[x1] = S[x];
			D[x2] = S[x];
			D[x1 + Pitch] = (S[x] >> 1) & 0x7F7F7F;
			D[x2 + Pitch] = (S[x] >> 1) & 0x7F7F7F;
		}
		D += Pitch * 2;
		S += W;
	}
}

void Interpolate2x(unsigned long *S,unsigned long *D,int W,int H,int Pitch)
{
	int x,y;

	for(x=0;x<W;x++) {
		register unsigned long p1,p2;
		register int x1,x2;

		x1 = x * 2 + 0;
		x2 = x * 2 + 1;

		p1 = S[x + 0];
		p2 = S[x + 1];

		D[x1] = p1;
		D[x2] = INTERPOLATE(p1,p2);
	}

	D += Pitch * 2;
	S += W;

	for(y=1;y<H;y++) {
		for(x=0;x<W;x++) {
			register unsigned long p1,p2;
			register int x1,x2;

			x1 = x * 2 + 0;
			x2 = x * 2 + 1;

			p1 = S[x + 0];
			p2 = S[x + 1];

			D[x1] = p1;
			D[x2] = INTERPOLATE(p1,p2);
		}
		D += Pitch * 2;
		S += W;
	}
}

void Television2x(unsigned long *S,unsigned long *D,int W,int H,int Pitch)
{
	int x,y;

	for(y=0;y<H;y++) {
		for(x=0;x<W;x++) {
			register unsigned long p1,p2,p3;
			register int x1,x2;

			x1 = x * 2 + 0;
			x2 = x * 2 + 1;

			p1 = S[x + 0];
			p2 = S[x + 1];
			p3 = INTERPOLATE(p1,p2);

			D[x1] = p1;
			D[x2] = p3;
			D[x1 + Pitch] = _50PERCENT(p1);
			D[x2 + Pitch] = _50PERCENT(p3);
		}
		D += Pitch * 2;
		S += W;
	}
}

void HQ2x(unsigned long *S,unsigned long *D,int W,int H,int Pitch)
{
//	hq2x_32((unsigned char*)S,(unsigned char*)D,W,H,Pitch);
}
